/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.flux.mybatis.search;

import com.arisgo.cloud.flux.mybatis.model.NumberIdWithTime;
import com.mybatisflex.core.constant.SqlConsts;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * @author Coulson
 * 2023-6-15
 */
public class Param {
    private static final Logger logger = LoggerFactory.getLogger(Param.class);

    private String field;

    private String expr = Expr.EQ.name();

    private Object value;

    public Param(String field, String expr, Object value) {
        this.field = field;
        this.expr = expr;
        this.value = value;
    }

    public Param() {
    }

    public static Param creator(String field, Object value) {
        return creator(field, Expr.EQ.name(), value);
    }

    public static Param creator(String field, String expr, Object value) {
        return new Param(field, expr, value);
    }

    public enum Expr {
        EQ(SqlConsts.EQUALS),
        NE(SqlConsts.NOT_EQUALS),

        GT(SqlConsts.GT),
        GE(SqlConsts.GE),
        LT(SqlConsts.LT),
        LE(SqlConsts.LE),
        IN(SqlConsts.IN),
        NOT_IN(SqlConsts.NOT_IN),
        NULL(SqlConsts.IS_NULL),
        LIKE(SqlConsts.LIKE),
        LIKE_LEFT(null),
        BETWEEN(SqlConsts.BETWEEN),
        ;
        private final String value;

        Expr(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public static Expr convert(String expr) {
            if (!StringUtils.hasLength(expr)) {
                return Expr.EQ;
            }
            try {
                return Expr.valueOf(expr.toUpperCase());
            } catch (IllegalArgumentException e) {
                for (Expr value : Expr.values()) {
                    if (expr.equalsIgnoreCase(value.value)) {
                        return value;
                    }
                }
                logger.error("not support expression: {}", expr);
                throw new IllegalArgumentException("not support expression: " + expr);
            }
        }
    }

    public void buildWrapper(QueryWrapper wrapper) {
        if (null == wrapper) {
            return;
        }
        QueryCondition condition;
        switch (Expr.convert(this.expr)) {
            case LT -> condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).lt(this.value);
            case LE -> condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).le(this.value);
            case GT -> condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).gt(this.value);
            case GE -> condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).ge(this.value);
            case IN ->
                    condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).in(convertArray(this.value));
            case NOT_IN ->
                    condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).notIn(convertArray(this.value));
            case BETWEEN ->
                    condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).between(convertArray(this.value));
            case NULL ->
                    condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).isNull(Boolean.TRUE.equals(this.value));
            case LIKE -> condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).like(this.value);
            case LIKE_LEFT ->
                    condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).likeLeft(this.value);
            case NE -> condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).ne(this.value);
            default -> condition = QueryMethods.column(StringUtil.camelToUnderline(this.field)).eq(this.value);
        }
        wrapper.where(condition);
    }

    public static void buildNotDelWrapper(QueryWrapper wrapper) {
        if (null == wrapper) {
            return;
        }
        wrapper.and(QueryMethods.column(NumberIdWithTime.IS_DELETED).eq(0));
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getExpr() {
        return expr;
    }

    public void setExpr(String expr) {
        this.expr = expr;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public Object[] convertArray(Object obj) {
        if (obj instanceof Collection<?> collection) {
            return collection.toArray();
        } else if (obj instanceof Object[] array) {
            return array;
        } else {
            return new Object[]{obj};
        }
    }

}
